/**
  gcc -shared ane.c -L"." -lFlashRuntimeExtensions -o test.dll
  */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "FlashRuntimeExtensions.h"

int mysystem(char *input, char *output, int maxlen)  
{  
	if( NULL==input || NULL==output )  
		return -1;  
	int reslen;  
	FILE *f;  
	memset(output, 0, maxlen);  
	//创建管道，并将input里的内容写入管道  
	f = popen(input, "r");  
	//从管理中读出数据，并写入output数组  
	reslen = fread(output, sizeof(char), maxlen, f);  
	pclose(f);
	return reslen;  
}  
FREObject test(FREContext ctx, void* funcData, uint32_t argc, FREObject argv[]) {
	uint32_t length; 
	const uint8_t* para1; 

	FREGetObjectAsUTF8((FREObject)argv[0], &length, &para1); 

	static char output[1024*128];
	mysystem((char*)para1,output,sizeof(output)-1);
	/*
	   int32_t para2; 
	   FREGetObjectAsInt32(argv[1], &para2);
	   */
	FREObject retObj;
	FRENewObjectFromUTF8(strlen(output), (const uint8_t *)output, &retObj);
	return retObj;
}
void ContextInitializer(void* extData, const uint8_t* ctxType, FREContext ctx, uint32_t* numFunctionsToTest, const FRENamedFunction** functionsToSet)
{
	*numFunctionsToTest = 1;
	FRENamedFunction* func = (FRENamedFunction*)malloc(sizeof(FRENamedFunction)*1);

	func[0].name = (const uint8_t*)"test";
	func[0].functionData = NULL;
	func[0].function = &test;

	*functionsToSet = func;
}

void ContextFinalizer(FREContext ctx) {
	return;
}

void ExtInitializer(void** extDataToSet, FREContextInitializer* ctxInitializerToSet, 
		FREContextFinalizer* ctxFinalizerToSet) {
	*extDataToSet = NULL;
	*ctxInitializerToSet = ContextInitializer;
	*ctxFinalizerToSet = ContextFinalizer;
}

void ExtFinalizer(void* extData) {
	return;
}

